package com.project.controller;

import com.project.bean.HistogramBean;
import com.project.service.IHistogramService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiresPermissions("salesTable")
@RequestMapping("histogram")
public class HistogramController {
    @Autowired
    private IHistogramService histogramService;
    @RequestMapping("findByItem")
    public List findByItem(Integer year) {
        List<HistogramBean> result=histogramService.findByItem(year);
        List newList = new ArrayList();
        newList.add(0, "月份");
        //循环得到类型名称  distinct：去重
        List rowOne = result.stream().map(n -> n.getType()).distinct().toList();
        for (int i = 0; i < rowOne.size(); i++) {
            newList.add(rowOne.get(i));
        }
//        ['mouth', '电脑产品', '安防产品', '打印扫描设备','办公耗材'],
//        ['一月', 500, 85.8, 93.7,211],
//        ['二月', 836, 73.4, 55.1,33],
//        ['三月', 86.4, 65.2, 82.5,444]
        List<Object[]> monthList = new ArrayList<>();
        monthList.add(newList.toArray());//添加第一列到传到前端的集合中
        for (int i = 0; i < 12; i++) {
            monthList.add(null);//12个月份为空
        }
        for (int i = 1; i < newList.size(); i++) {//根据产品类别循环并填充数据
            for (int j = 0; j < result.size(); j++) {//根据查出来多少条数据对象循环
                HistogramBean histogram = result.get(j);
                if (histogram.getType().equals(newList.get(i))) {//产品类别判断
                    for (int k = 1; k <= 12; k++) {//添加月份的数据
                        if (histogram.getMonth().equals(k + "月")) {
                            if (monthList.get(k) == null) {
                                Object[] list = new Object[newList.size()];
                                list[0] = k + "月";
                                list[i] = histogram.getNum();
                                monthList.set(k, list);
                            } else {
                                monthList.get(k)[i] = histogram.getNum();
                            }
                            result.remove(j);//将已取出的数据删除
                        }
                    }

                }
            }
        }
        for (int i = 0; i < monthList.size(); i++) {
            if (monthList.get(i) == null) {
                monthList.remove(i);
                i--;
                continue;
            }
            else {
                for (int j = 0; j < monthList.get(i).length; j++) {
                    if (monthList.get(i)[j]==null){
                        monthList.get(i)[j]=0;
                    }
                }
            }
        }

//            for (int i=0; i<result.size(); i++){
//                newList.add(result.get(i).getType());
//            }

//            System.out.println(result);
//            for (int i=0; i<result.size(); i++){
//                List newListT
//            }
//        List a = List.of(Arrays.stream(monthList.get(0)).toArray());
        return monthList;


    }
}
